﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCNet.Jabber.Plugin;
using NUnit.Framework;
using System.Diagnostics;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Remote;


namespace ccnet.jabber.plugin.test
{
    /*
     * 
     *      <jabber>
     *          <host>xserver.com</host>
     *          <port>5222</port>
     *          <username>f.l@xservercom</username>
     *          <password>pass@123</password>
     *          <recipients>
     *              <recipient jid='jid1' />
     *              <recipient jid='jid2' />
     *              <recipient jid='jid3' />
     *          </recipients>
     *          <message>Test message</message>
     *      </jabber>
     * 
     * 
     * 
     */

    [TestFixture]
    public class JabberPublisherTaskTest
    {
        [Test]
        public void SendXmppMessage()
        {
            JabberPublisherTask task = new JabberPublisherTask();

            task.Username = "sudhanshu.s@directi.com";
            task.Password = "tricky";
            task.Host = "directi.com";
            task.Recipients = new Recipient[]
                                  {new Recipient("amit.mi@directi.com"), new Recipient("amit.mi@directi.com")};


            task.SendMessage();

        }

        private JabberPublisherTask GetJabberTaskThroughXML()
        {
            string xml = "<jabber>" +
             "<host>xserver.com</host>" +
             "<username>f.l@xservercom</username>" +
             " <password>pass@123</password>" +
             " <recipients>" +
             "    <recipient jid='jid1' />" +
             "   <recipient jid='jid2' />" +
             "   <recipient jid='jid3' />" +
             "</recipients>" +
             " <message>Test message</message>" +
             " </jabber>";

            Debug.Write(xml);

            return  NetReflector.Read(xml) as JabberPublisherTask;

        }

        private JabberPublisherTask GetJabberTaskThroughXML_WithoutMessage()
        {
            string xml = "<jabber>" +
             "<host>xserver.com</host>" +
             "<username>f.l@xservercom</username>" +
             " <password>pass@123</password>" +
             " <recipients>" +
             "    <recipient jid='jid1' />" +
             "   <recipient jid='jid2' />" +
             "   <recipient jid='jid3' />" +
             "</recipients>" +
             " </jabber>";

            Debug.Write(xml);

            return NetReflector.Read(xml) as JabberPublisherTask;

        }

        private JabberPublisherTask GetJabberTaskThroughXML_WithoutCondition()
        {
            string xml = "<jabber>" +
             "<host>xserver.com</host>" +
             "<username>f.l@xservercom</username>" +
             " <password>pass@123</password>" +
             " <recipients>" +
             "    <recipient jid='jid1' />" +
             "   <recipient jid='jid2' />" +
             "   <recipient jid='jid3' />" +
             "</recipients>" +
             " </jabber>";

            Debug.Write(xml);

            return NetReflector.Read(xml) as JabberPublisherTask;

        }

        private JabberPublisherTask GetJabberTaskThroughXML_WithMinimalConfig_MultipleRecipients()
        {
            string xml = "<jabber>" +
             "<username>sudhanshu.s@directi.com</username>" +
             " <password>tricky</password>" +
             " <recipients>" +
             "    <recipient jid='amit.mi@directi.com' />" + 
             "    <recipient jid='vinodkumar.n@directi.com' />" + 
             "</recipients>" +
             " </jabber>";

            Debug.Write(xml);

            return NetReflector.Read(xml) as JabberPublisherTask;
        }


        private JabberPublisherTask GetJabberTaskThroughXML_WithMinimalConfig_SpecificCondition()
        {
            string xml = "<jabber>" +
             "<username>sudhanshu.s@directi.com</username>" +
             " <password>tricky</password>" +
             " <recipients>" +
             "    <recipient jid='amit.mi@directi.com' />" +
             "</recipients>" +
             " <condition>Failure</condition>" +
             " </jabber>";

            Debug.Write(xml);

            return NetReflector.Read(xml) as JabberPublisherTask;
        }

        [Test]
        public void TestConfigurationElements()
        {

            JabberPublisherTask result = GetJabberTaskThroughXML();

            Assert.AreEqual(result.Host,"xserver.com");
            Assert.AreEqual(result.Password,"pass@123");
            Assert.AreEqual(result.Recipients.Count(),3);
        }

        [Test]
        public void TestJabberTaskWithMinimalConfig()
        {

            JabberPublisherTask jabberTask = GetJabberTaskThroughXML_WithMinimalConfig_MultipleRecipients();

            IIntegrationResultMockObject integrationResultMock = new IIntegrationResultMockObject()
                                                               {
                                                                   Status = IntegrationStatus.Success,
                                                                   IntegrationRequest =
                                                                       new IntegrationRequest(
                                                                       BuildCondition.ForceBuild, "TestRun",
                                                                       "sampleUser"),
                                                                    ProjectName = "sampleProject"
                                                               };

            jabberTask.Run(integrationResultMock);
        }

        [TestCase(IntegrationStatus.Success)]
        //[TestCase(IntegrationStatus.Failure)]
        public void TestJabberTaskWithSpecificIntegrationCondition(IntegrationStatus integrationStatus )
        {

            JabberPublisherTask jabberTask = GetJabberTaskThroughXML_WithMinimalConfig_MultipleRecipients();

            IIntegrationResultMockObject integrationResultMock = new IIntegrationResultMockObject()
            {
                Status = integrationStatus,
                IntegrationRequest =
                    new IntegrationRequest(
                    BuildCondition.ForceBuild, "TestRun",
                    "sampleUser"),
                ProjectName = "sampleProject"
            };

            jabberTask.Run(integrationResultMock);
        }
    }
}